package com.fy.module.sys.controller;

import com.fy.module.sys.common.vo.ResultVO;
import com.fy.module.sys.entity.UserMsg;
import com.fy.module.sys.service.IUserService;
import com.fy.utils.JwtUtils;
import com.fy.utils.RsaUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;
import com.fy.common.dto.LoginDto;
import com.fy.common.vo.ApiRest;
import com.fy.module.sys.entity.UserMsg;
import com.fy.module.sys.service.impl.UserServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import javax.annotation.security.PermitAll;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.util.List;

/**
 * <p>
 * 用户信息表 前端控制器
 * </p>
 *
 * @author jiang
 * @since 2023-03-21
 */
@RestController
@Api(description = "用户管理")
@CrossOrigin
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserServiceImpl userService;

    /**
     * 用户登录
     */
    @PostMapping("/login")
    @ApiOperation("用户登录")
    public ApiRest userLogin(@RequestBody LoginDto loginDto, HttpServletResponse response){
     ApiRest apiRest = userService.userLogin(loginDto,response);
        return apiRest;
    }

    /**
     * 用户访问资源时，检查用户所携带的token是否合法
     */
    @GetMapping("/checkToken")
    @ApiOperation("检查token是否合法")
    public ApiRest checkToken(@RequestHeader("token") String token){
        ApiRest apiRest = userService.parseToken(token);
        return apiRest;
    }



    @ApiOperation("用户个人信息")//@ApiModelProperty("用户个人信息")
    @GetMapping("/list")
    public ResultVO user(@ApiParam(value = "用户id") @RequestParam String userId){
        ResultVO userInfo = userService.findUserAllByUserId(Integer.parseInt(userId));
        if (userInfo.getCode().equals(1)){
            return new ResultVO(1,"查询失败");
        }
        return new ResultVO(0,"查询成功",userInfo);

    }



    @ApiOperation("用户修改的信息--基本资料修改")
    @PostMapping("/updateUserMsg")
    public ResultVO updateUserMsg(@ApiParam(value = "修改的信息") @RequestParam String account,@RequestParam String phone,@RequestParam String email,@RequestParam String sex,@RequestParam String userId){
        return userService.updateUser(account,phone,email,sex,userId);
    }

    @ApiOperation("密码修改信息")
    @PostMapping("/updatePassword")
    public ResultVO updatePassword(@ApiParam(value = "修改密码的信息") @RequestParam String userId, @RequestParam String oldPassword, @RequestParam String newPassword1, @RequestParam String newPassword2){


        return userService.updatePassword(userId,oldPassword,newPassword1,newPassword2);
    }

    @GetMapping("/getUserMsg")
    public ApiRest getUserMsg(@RequestHeader("token") String token){
        PublicKey publicKey=null;
        //调用RsaUtils获取工具类
        try {
            publicKey= RsaUtils.getPublicKey(ResourceUtils.getFile("classpath:pub").getPath());
        } catch (Exception e) {
            e.printStackTrace();
        }


        //通过JWTUtils解析token令牌，获取里面的用户信息
        UserMsg user = (UserMsg) JwtUtils.getInfoFromToken(token, publicKey, UserMsg.class);
        if(user==null){
            return ApiRest.failure("token为空");
        }
        return ApiRest.success("token解析成功",user);
    }

}
